<?php
class ShopController extends Controller{
	private $model;
	public function __construct(){
		parent::__construct();
		$this->model=K("Goods");
		
	}
	public function index(){
		$goodsData=$this->model->all();

//		压入小图
        foreach($goodsData as $k=>$v){
//      	通过goods_gid获得商城小图
            $gid=$v['gid'];
			
            $shopData=K('Details')->where("goods_gid='{$gid}'")->find();
			$shoppic=$shopData['shoppic'];
			
        	$type=strchr($shoppic,'.');
//
			$smallpic=str_replace($type,'_small'.$type,$shoppic);
//		
        	$goodsData[$k]['smallpic']=$smallpic;
        }

		$this->assign("goodsData",$goodsData);
		
		$this->display();
	}
	public function add(){
		if(IS_POST){
			p($_POST['shopPic']);
		
			$cid=$_POST['category_id'];
			
			$tid=K("Cate")->where("id='{$cid}'")->find();
			$tid=$tid['type_id'];
			$_POST['type_id']=$tid;
			
//			将提交的数据添加给3张表,在Goods模型中添加另外两张表
//      把添加商品选中的状态存入到缓存
        
        
        if(!$gid=$this->model->addData()){
        	$this->error($this->model->error);	
        }
		
//		把添加商品选中的状态，存入到缓存，下次编辑的时候就可以直接调用了
		S('selected_' . $gid,$_POST['selected']);
//		$this->success("添加成功");


		}
//		处理分类数据
        $cateData=M("category")->all();
		$cateData=Data::tree($cateData,'cname','id');
		$this->assign("cateData",$cateData);
//		处理品牌数据
        $brandData=M("brand")->all();
		
		$this->assign("brandData",$brandData);
		$this->display();
	}
	public function edit(){
		$gid = Q('get.gid',0,'intval');
		if(IS_POST){
//			p($_POST);
			if(!$this->model->updateData($gid)){
				$this->error($this->model->error);
				
			}
//			$this->success("编辑成功");
			
		}

		
//		读取缓存
        $selected=S('selected_'.$gid);
		
		$this->assign('selected',$selected);
		$gid=$_GET['gid'];
//		获得旧数据
        $oldData=$this->model->where("gid=$gid")->find();
//		处理商城图册小图
		$shopData=K('Details')->where("goods_gid='{$gid}'")->find();
	    $shoppic=$shopData['shoppic'];
			
        $type=strchr($shoppic,'.');
//
	    $shoppic=str_replace($type,'_small'.$type,$shoppic);
		$oldData['shoppic']=$shoppic;
//		商城图册
	
		
		
		$this->assign('oldData',$oldData);
		//		处理分类数据
        $cateData=M("category")->all();
		$cateData=Data::tree($cateData,'cname','id');
		$this->assign("cateData",$cateData);
//		处理品牌数据
        $brandData=M("brand")->all();
//	    处理详情 

		$this->assign("brandData",$brandData);
		$details=K('Details')->where("goods_gid='{$gid}'")->find();

	
	
		$this->assign('details',$details);
		
	
		$this->display();
	}
	public function getTypeAttr(){
		if(!IS_AJAX)return false;
		$cid=$_POST['cid'];
//		通过$cid可以知道type_id,通过type_id可以找到type_attr,
        $attrData=M()->JOIN("__category__  c JOIN __typeproperty__ tp ON c.type_id=tp.type_id")->where("c.id='{$cid}'")->all();
//		将类型属性的值按照,来拆分
        foreach($attrData as $k=> $v){
        	$attrData[$k]['propertyvalue']=explode(',',$v['propertyvalue']);
	
        }
		echo json_encode($attrData);
		exit;
	}
	public function uploadList(){
		$upload=new Upload('Upload/content'.date('y/m'));
		$file=$upload->upload();
		if(empty($file)){
			$this->ajax('上传失败');
		}else{
			$data=$file[0];
			$this->ajax($data);
		}
		
	}
	public function uploadPic(){
		$upload=new Upload('Upload/content'.date('y/m'));
		$file = $upload->upload();
		if(empty($file)){
			$this->ajax("上传失败");
			
		}else{
//			上传成功
	        $type=strrchr($file[0]['path'],'.');
			$img=new Image();
			$smallPath = str_replace($type, '_small' . $type, $file[0]['path']);
			$img->thumb($file[0]['path'],$smallPath,200,200,2);
			$midPath=str_replace($type,"_mid".$type,$file[0]['path']);
			$img->thumb($file[0]['path'],$midPath,400,400,2);
			$bigPath=str_replace($type,"_big".$type,$file[0]['path']);
			$img->thumb($file[0]['path'],$bigPath,800,800,2);
//			这两句什么意思
			$data=$file[0];
		
			$this->ajax($data);
//			上一句相当于echo json_encode 其实就是通过ajax返回数据
			 
		}
		
	}
	public function del(){
		$gid=Q('get.gid',0,'intval');
		$this->model->delData($gid);
		$this->success("删除成功");
		
	}
}


 ?>